package sjtu.ist.warehouse4ws.tool;


import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;

import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;





import sjtu.ist.warehouse4ws.model.Department;


import sjtu.ist.warehouse4ws.model.Resource;
import sjtu.ist.warehouse4ws.model.Role;
import sjtu.ist.warehouse4ws.model.Rolepermissions;
import sjtu.ist.warehouse4ws.model.Permission;
import sjtu.ist.warehouse4ws.model.Permissionresources;



public class ParserOrg {
	public List<Department> parseDepartment(String rootPath){
		
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		try {
			
			DocumentBuilder db = dbf.newDocumentBuilder();
            
			//Change2UTF.change(rootPath); // change other coded file to UTF-8
			File file = new File(rootPath);
			BufferedReader br= new BufferedReader(new InputStreamReader(new FileInputStream(file),"gb2312"));//newly added
			InputSource is = new InputSource(br);//newly added
			Document doc=db.parse(is);//Document doc = db.parse(file);
			doc.normalize();
//			ChangeCharset test = new ChangeCharset();
//			doc.setXMLEncoding("gbk");
//			Document dom1=DocumentHelper.createDocument();//创建xml文件
//		     dom1.setXMLEncoding("gbk");
			List<Department> dept=new ArrayList<Department>();
			List<String> organizationTagName=new ArrayList<String>();
			organizationTagName.add("edu.sjtu.erplab.modeling.model.organization.Enterprise");
			organizationTagName.add("edu.sjtu.erplab.modeling.model.organization.Department");
			organizationTagName.add("edu.sjtu.erplab.modeling.model.organization.Position");
			
			String parentName=null;
			
			for(int k=0;k<organizationTagName.size();k++){
				NodeList nl1= doc.getElementsByTagName(organizationTagName.get(k));
				int size1=nl1.getLength();
				for(int i=0;i<size1;i++){
					Node n=nl1.item(i);
					NodeList nl2=n.getChildNodes();
					int size2=nl2.getLength();
					Department deptunit=new Department();
					for(int j=0;j<size2;j++){
						Node n2=nl2.item(j);
						
						if(n2.hasChildNodes()){
							
							if(n2.getNodeName()=="id"){				
								deptunit.setId(n2.getFirstChild().getNodeValue());
								if(k==0/*&&(!n2.getFirstChild().getNodeValue().equals(null))*/)
									parentName=n2.getFirstChild().getNodeValue();
							}
							if(n2.getNodeName()=="cnName"){
								
								deptunit.setCnName(n2.getFirstChild().getNodeValue());
							}
							if(n2.getNodeName()=="enName")
								deptunit.setEnName(n2.getFirstChild().getNodeValue());
							/*if(n2.getNodeName()=="parentName"){*/
								if(k!=0)
									deptunit.setParentName(parentName);
								else
									deptunit.setParentName("root");
								
							//}
						}	
					}	dept.add(deptunit);	
				}
			}
			return dept;
			
		}
			catch(ParserConfigurationException ex){
				ex.printStackTrace();
			}
			catch(IOException ex){
				ex.printStackTrace();
			}
			catch(SAXException ex){
				ex.printStackTrace();
			}	
			catch(Exception e){
				e.printStackTrace();
			}
			return null;
	}
	public List<Permission> parsePermission(String rootPath){
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		try {
            DocumentBuilder db = dbf.newDocumentBuilder();
            
			//Change2UTF.change(rootPath); // change other coded file to UTF-8
			File file = new File(rootPath);
			BufferedReader br= new BufferedReader(new InputStreamReader(new FileInputStream(file),"gb2312"));//newly added
			InputSource is = new InputSource(br);//newly added
			Document doc=db.parse(is);//Document doc = db.parse(file);
			doc.normalize();
			List<Permission> permission=new ArrayList<Permission>();
			NodeList nl1=doc.getElementsByTagName("edu.sjtu.erplab.modeling.model.organization.Permission");
			int size1=nl1.getLength();
			for(int i=0;i<size1;i++){
				Node n=nl1.item(i);
				NodeList nl2=n.getChildNodes();
				int size2=nl2.getLength();
				Permission permissionunit=new Permission();
				for(int j=0;j<size2;j++){
					Node n2=nl2.item(j);
					if(n2.hasChildNodes()){
						
						//System.out.println(n2.getNodeName()+"="+n2.getFirstChild().getNodeValue());
						if(n2.getNodeName()=="id")
							permissionunit.setId(n2.getFirstChild().getNodeValue());
						if(n2.getNodeName()=="enName")
							permissionunit.setEnName(n2.getFirstChild().getNodeValue());
						if(n2.getNodeName()=="cnName")
							permissionunit.setCnName(n2.getFirstChild().getNodeValue());
						if(n2.getNodeName()=="description")
							permissionunit.setDescription(n2.getFirstChild().getNodeValue());
						if(n2.getNodeName()=="permissionPackage")
							permissionunit.setPermissionPackage(n2.getFirstChild().getNodeValue());
											
					}			
				}permission.add(permissionunit);	
			}
			return permission;
		}catch(ParserConfigurationException ex){
			ex.printStackTrace();
		}
		catch(IOException ex){
			ex.printStackTrace();
		}
		catch(SAXException ex){
			ex.printStackTrace();
		}	
		catch(Exception e){
			e.printStackTrace();
		}
		return null;
	}
	
	public List<Resource> parseResource(String rootPath){
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		try {
			DocumentBuilder db = dbf.newDocumentBuilder();


            
			//Change2UTF.change(rootPath); // change other coded file to UTF-8
			File file = new File(rootPath);
			BufferedReader br= new BufferedReader(new InputStreamReader(new FileInputStream(file),"gb2312"));//newly added
			InputSource is = new InputSource(br);//newly added
			Document doc=db.parse(is);//Document doc = db.parse(file);
			doc.normalize();
			List<Resource> resources=new ArrayList<Resource>();
			NodeList nl11=doc.getElementsByTagName("edu.sjtu.erplab.modeling.model.organization.ResourceFunction");
			int size11=nl11.getLength();
			NodeList nl12=doc.getElementsByTagName("edu.sjtu.erplab.modeling.model.organization.ResourceDomain");
			int size12=nl12.getLength();
			for(int i=0;i<size11;i++){
				Node n=nl11.item(i);
				NodeList nl2=n.getChildNodes();
				int size2=nl2.getLength();
				Resource resourceunit=new Resource();
				for(int j=0;j<size2;j++){
					Node n2=nl2.item(j);
					if(n2.hasChildNodes()){
						
						//System.out.println(n2.getNodeName()+"="+n2.getFirstChild().getNodeValue());
							resourceunit.setType("ResourceFunction");
						if(n2.getNodeName()=="id")
							resourceunit.setId(n2.getFirstChild().getNodeValue());
						if(n2.getNodeName()=="enName")
							resourceunit.setEnName(n2.getFirstChild().getNodeValue());
						if(n2.getNodeName()=="cnName")
							resourceunit.setCnName(n2.getFirstChild().getNodeValue());
						if(n2.getNodeName()=="description")
							resourceunit.setDescription(n2.getFirstChild().getNodeValue());
						if(n2.getNodeName()=="serviceName")
							resourceunit.setServiceName(n2.getFirstChild().getNodeValue());
						if(n2.getNodeName()=="methodName")
							resourceunit.setMethodName(n2.getFirstChild().getNodeValue());
						if(n2.getNodeName()=="functionPointId")
							resourceunit.setFunctionId(n2.getFirstChild().getNodeValue());
											
					}			
				}resources.add(resourceunit);	
			}
			for(int l=0;l<size12;l++){
				Node n=nl12.item(l);
				NodeList nl2=n.getChildNodes();
				int size2=nl2.getLength();
				Resource resourceunit=new Resource();
				for(int m=0;m<size2;m++){
					Node n2=nl2.item(m);
					if(n2.hasChildNodes()){
						
						//System.out.println(n2.getNodeName()+"="+n2.getFirstChild().getNodeValue());
							resourceunit.setType("ResourceDomain");
						if(n2.getNodeName()=="id")
							resourceunit.setId(n2.getFirstChild().getNodeValue());
						if(n2.getNodeName()=="enName")
							resourceunit.setEnName(n2.getFirstChild().getNodeValue());
						if(n2.getNodeName()=="cnName")
							resourceunit.setCnName(n2.getFirstChild().getNodeValue());
						if(n2.getNodeName()=="description")
							resourceunit.setDescription(n2.getFirstChild().getNodeValue());
						if(n2.getNodeName()=="serviceName")
							resourceunit.setServiceName(n2.getFirstChild().getNodeValue());
						if(n2.getNodeName()=="methodName")
							resourceunit.setMethodName(n2.getFirstChild().getNodeValue());
						if(n2.getNodeName()=="domainModelId")
							resourceunit.setFunctionId(n2.getFirstChild().getNodeValue());
											
					}			
				}resources.add(resourceunit);	
			}
			return resources;
		}catch(ParserConfigurationException ex){
			ex.printStackTrace();
		}
		catch(IOException ex){
			ex.printStackTrace();
		}
		catch(SAXException ex){
			ex.printStackTrace();
		}	
		catch(Exception e){
			e.printStackTrace();
		}
		return null;
	}
	
	public List<Rolepermissions> parseRolepermissions(String rootPath){
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		try {
DocumentBuilder db = dbf.newDocumentBuilder();
            
			//Change2UTF.change(rootPath); // change other coded file to UTF-8
			File file = new File(rootPath);
			BufferedReader br= new BufferedReader(new InputStreamReader(new FileInputStream(file),"gb2312"));//newly added
			InputSource is = new InputSource(br);//newly added
			Document doc=db.parse(is);//Document doc = db.parse(file);
			doc.normalize();
			List<Rolepermissions> rolepermissions=new ArrayList<Rolepermissions>();
			NodeList nl1=doc.getElementsByTagName("role");
			int size1=nl1.getLength();
			for(int i=0;i<size1;i++){
				Node n=nl1.item(i);
				NodeList nl2=n.getChildNodes();
				int size2=nl2.getLength();
				Rolepermissions rolepermissionunit=new Rolepermissions();
				String roleid=null;
				for(int j=0;j<size2;j++){
					Node n2=nl2.item(j);
					if(n2.hasChildNodes()){	
						
						//System.out.println(n2.getNodeName()+"="+n2.getFirstChild().getNodeValue());
						if(n2.getNodeName()=="id"){
							roleid=n2.getFirstChild().getNodeValue();
							//System.out.println(n2.getNodeName()+"="+n2.getFirstChild().getNodeValue());
							rolepermissionunit.setRoleid(roleid/*n2.getFirstChild().getNodeValue()*/);}
						if(n2.getNodeName()=="permissionsIds"){
							NodeList nl3=n2.getChildNodes();
							int size3=nl3.getLength();
							for(int l=0;l<size3;l++){
								Node n3=nl3.item(l);
								if(n3.hasChildNodes()){
									if(n3.getNodeName()=="string"){
										rolepermissionunit.setPermissionid(n3.getFirstChild().getNodeValue());
										rolepermissions.add(rolepermissionunit);
										rolepermissionunit=new Rolepermissions(null,roleid);
										//System.out.println("permissionid="+n3.getFirstChild().getNodeValue());
									}
										
								}
							}
						}					
					}			
				};	
			}
			return rolepermissions;
		}catch(ParserConfigurationException ex){
			ex.printStackTrace();
		}
		catch(IOException ex){
			ex.printStackTrace();
		}
		catch(SAXException ex){
			ex.printStackTrace();
		}	
		catch(Exception e){
			e.printStackTrace();
		}
		return null;
	}
	
	public List<Permissionresources> parsePermissionresources(String rootPath){
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		try {
DocumentBuilder db = dbf.newDocumentBuilder();
            
			//Change2UTF.change(rootPath); // change other coded file to UTF-8
			File file = new File(rootPath);
			BufferedReader br= new BufferedReader(new InputStreamReader(new FileInputStream(file),"gb2312"));//newly added
			InputSource is = new InputSource(br);//newly added
			Document doc=db.parse(is);//Document doc = db.parse(file);
			doc.normalize();
			List<Permissionresources> permissionresources=new ArrayList<Permissionresources>();
			NodeList nl1=doc.getElementsByTagName("edu.sjtu.erplab.modeling.model.organization.Permission");
			int size1=nl1.getLength();
			for(int i=0;i<size1;i++){
				Node n=nl1.item(i);
				NodeList nl2=n.getChildNodes();
				int size2=nl2.getLength();
				Permissionresources permissionresourceunit=new Permissionresources();
				for(int j=0;j<size2;j++){
					Node n2=nl2.item(j);
					if(n2.hasChildNodes()){
						
						//System.out.println(n2.getNodeName()+"="+n2.getFirstChild().getNodeValue());
						if(n2.getNodeName()=="id")
							permissionresourceunit.setPermissionid(n2.getFirstChild().getNodeValue());
							//System.out.println(n2.getNodeName()+"="+n2.getFirstChild().getNodeValue());
							//rolepermissionunit.setRoleid(n2.getFirstChild().getNodeValue());
						if(n2.getNodeName()=="resourceFuncs"){
							NodeList nl3=n2.getChildNodes();
							int size3=nl3.getLength();
							for(int l=0;l<size3;l++){
								Node n3=nl3.item(l);
								if(n3.hasChildNodes()){
									if(n3.getNodeName()=="edu.sjtu.erplab.modeling.model.organization.ResourceFunction") {
										NodeList nl4=n3.getChildNodes();
										int size4=nl4.getLength();
										for(int m=0;m<size4;m++){
											Node n4=nl4.item(m);
											if(n4.hasChildNodes()){
												if(n4.getNodeName()=="id"){
													permissionresourceunit.setResourceid(n4.getFirstChild().getNodeValue());
													permissionresources.add(permissionresourceunit);
												}
													//System.out.println("resoureid="+n4.getFirstChild().getNodeValue());
											}
										
										//rolepermissionunit.setPermissionid(n3.getFirstChild().getNodeValue());
										//rolepermissions.add(rolepermissionunit);
										}
									}
										//System.out.println("permissionid="+n3.getFirstChild().getNodeValue());
								}
							}
						}
						else if(n2.getNodeName()=="resourceDMs"){
							NodeList nl3=n2.getChildNodes();
							int size3=nl3.getLength();
							for(int l=0;l<size3;l++){
								Node n3=nl3.item(l);
								if(n3.hasChildNodes()){
									if( n3.getNodeName()=="edu.sjtu.erplab.modeling.model.organization.ResourceDomain"){
										NodeList nl4=n3.getChildNodes();
										int size4=nl4.getLength();
										for(int m=0;m<size4;m++){
											Node n4=nl4.item(m);
											if(n4.hasChildNodes()){
												if(n4.getNodeName()=="id"){
													permissionresourceunit.setResourceid(n4.getFirstChild().getNodeValue());
													permissionresources.add(permissionresourceunit);
												}
													//System.out.println("resoureid="+n4.getFirstChild().getNodeValue());
											}
										
										//rolepermissionunit.setPermissionid(n3.getFirstChild().getNodeValue());
										//rolepermissions.add(rolepermissionunit);
										}
									}
										//System.out.println("permissionid="+n3.getFirstChild().getNodeValue());
								}
							}
						}
						
					}			
				};	
			}
			return permissionresources;
		}catch(ParserConfigurationException ex){
			ex.printStackTrace();
		}
		catch(IOException ex){
			ex.printStackTrace();
		}
		catch(SAXException ex){
			ex.printStackTrace();
		}	
		catch(Exception e){
			e.printStackTrace();
		}
		return null;
	}
	
	public List<Role> parseRole(String rootPath){
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		try {
DocumentBuilder db = dbf.newDocumentBuilder();
            
			//Change2UTF.change(rootPath); // change other coded file to UTF-8
			File file = new File(rootPath);
			BufferedReader br= new BufferedReader(new InputStreamReader(new FileInputStream(file),"gb2312"));//newly added
			InputSource is = new InputSource(br);//newly added
			Document doc=db.parse(is);//Document doc = db.parse(file);
			doc.normalize();
			List<Role> role=new ArrayList<Role>();
			NodeList nl1=doc.getElementsByTagName("role");
			int size1=nl1.getLength();
			for(int i=0;i<size1;i++){
				Node n=nl1.item(i);
				NodeList nl2=n.getChildNodes();
				int size2=nl2.getLength();
				Role roleunit=new Role();
				for(int j=0;j<size2;j++){
					Node n2=nl2.item(j);
					if(n2.hasChildNodes()){
						
						//System.out.println(n2.getNodeName()+"="+n2.getFirstChild().getNodeValue());
						if(n2.getNodeName()=="id")
							roleunit.setId(n2.getFirstChild().getNodeValue());
						if(n2.getNodeName()=="enName")
							roleunit.setEnName(n2.getFirstChild().getNodeValue());
						if(n2.getNodeName()=="cnName")
							roleunit.setCnName(n2.getFirstChild().getNodeValue());
						if(n2.getNodeName()=="description")
							roleunit.setDescription(n2.getFirstChild().getNodeValue());
						if(n2.getNodeName()=="rolePackage")
							roleunit.setRolePackage(n2.getFirstChild().getNodeValue());
						if(n2.getNodeName()=="roleType")
							roleunit.setRoleType(n2.getFirstChild().getNodeValue());
						if(n2.getNodeName()=="parentRoleId")
							roleunit.setParentRoleId(n2.getFirstChild().getNodeValue());
											
					}			
				}role.add(roleunit);	
			}
			return role;
		}catch(ParserConfigurationException ex){
			ex.printStackTrace();
		}
		catch(IOException ex){
			ex.printStackTrace();
		}
		catch(SAXException ex){
			ex.printStackTrace();
		}	
		catch(Exception e){
			e.printStackTrace();
		}
		return null;
	}
	
	public static void main(String[] args) throws IOException, Exception {
		
		String a="123";
		String b=a;
		a="456";
		System.out.println(b);


		//List<Role> role = test.parseRole(outFile);
		//List<Rolepermissions> rolepermissions = test.parseRolepermissions(outFile);
		//List<Permission> permission = test.parsePermission(outFile);
		//List<Permissionresources> permissionresources = test.parsePermissionresources(outFile);
		//List<Resource> resource = test.parseResource(outFile);
		
		
 
//		String outfile="C:/Users/eric/Desktop/Organizations.org";
//		
//		
//		ParserOrg test=new ParserOrg();
//		List<Department> dept=test.parseDepartment(outfile);
//		System.out.println("Department Parsing:");
//		for(int i=0;i<dept.size();i++)
//			System.out.println(dept.get(i).getId()+" "+dept.get(i).getEnName()+" "+dept.get(i).getCnName()+" "+dept.get(i).getParentName());
//		List<Role> role=test.parseRole(outfile);
//		System.out.println("\nRole Parsing:");
//		for(int i=0;i<role.size();i++)
//			System.out.println(role.get(i).getId()+" "+role.get(i).getEnName()+" "+role.get(i).getCnName()+" "+role.get(i).getDescription()+" "+role.get(i).getRolePackage()+" "+role.get(i).getRoleType()+" "+role.get(i).getParentRoleId());
//		List<Rolepermissions> rolepermissions=test.parseRolepermissions(outfile);
//		System.out.println("\nRolepermissions Parsing:");
//		for(int i=0;i<rolepermissions.size();i++)
//			System.out.println(rolepermissions.get(i).getRoleid()+" "+rolepermissions.get(i).getPermissionid());
//		List<Permission> permission=test.parsePermission(outfile);
//		System.out.println("\nPermission Parsing:");
//		for(int i=0;i<permission.size();i++)
//			System.out.println(permission.get(i).getId()+" "+permission.get(i).getEnName()+" "+permission.get(i).getCnName()+" "+permission.get(i).getDescription()+" "+permission.get(i).getPermissionPackage());
//		List<Permissionresources> permissionresources=test.parsePermissionresources(outfile);
//		System.out.println("\nPermissionresources Parsing:");
//		for(int i=0;i<permissionresources.size();i++)
//			System.out.println(permissionresources.get(i).getPermissionid()+" "+permissionresources.get(i).getResourceid());
//		List<Resource> resources=test.parseResource(outfile);
//		System.out.println("\nResource Parsing:");
//		for(int i=0;i<resources.size();i++)
//			System.out.println(resources.get(i).getId()+" "+resources.get(i).getEnName()+" "+resources.get(i).getCnName()+" "+resources.get(i).getDescription()+" "+resources.get(i).getMethodName()+" "+resources.get(i).getServiceName()+" "+resources.get(i).getFunctionId());



	}

}

